window.onload = function () {
    document.getElementById("role_list").className = "add_class";
    document.getElementById("system-management").className = "add-nav-active";
    document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
    initialize()
};

function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}


function initialize(page=1) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/role_list.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], 1);
            }
        }
    };
}

// 动态创建列表
function create_table(count_num, json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    var json_dict = {};
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");
        td.innerHTML = "<input id='"+json_data[i]["id"]+"' class='check_box' type=\"checkbox\">";td.style.background="#f8f9fa";td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["character"];td1.title = json_data[i]["character"];td1.style.textAlign = "center";
        td2.innerHTML = json_data[i]["permission"];td2.title = json_data[i]["permission"];
        td3.innerHTML = json_data[i]["remark"];td3.title = json_data[i]["remark"];
        td4.innerHTML = "<a id='"+json_data[i]["character"]+"' href='#' onclick='view_details(this.id)'  data-toggle=\"modal\" data-target=\".bd-example-modal-xl1\">详情</a>";td4.title = json_data[i]["remark"];td4.style.textAlign = "center";
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);
        t_table.appendChild(tr);
        json_dict[json_data[i]["character"]] = {"permission": json_data[i]["permission"].split(","), "remark": json_data[i]["remark"]}
    }
    window.JSONdetail = json_dict;
}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

// 加载分页条
function pagenum(dataTotals=0, page=1) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            initialize(page);
            document.getElementById("check_all").checked = false;
        }
    });
}

// 全选与反全选
function check_all_not() {
    var check_box_list = document.getElementsByClassName("check_box");
    if (document.getElementById("check_all").checked) {
        for (var t=0; t<check_box_list.length; t++) {
            check_box_list[t].checked = true;
        }
    }else {
        for (var j=0; j<check_box_list.length; j++) {
            check_box_list[j].checked = false;
        }
    }
}

// 查看角色详情
function view_details(ele_id) {
    var JSONdetail = window.JSONdetail;
    console.log(JSONdetail[ele_id]);
    var model_character = document.getElementById("model-character");
    var model_remark = document.getElementById("model-remark");
    var model_permission = document.getElementById("model-permission");
    model_character.innerHTML = "";model_permission.innerHTML = "";model_remark.innerHTML = "";
    var character_p = document.createElement("p");
    character_p.className = "detail_p";
    character_p.innerHTML = "<a href='#'>"+ele_id+"</a>";
    model_character.appendChild(character_p);
    for (var c=0; c<JSONdetail[ele_id]["permission"].length; c++) {
        var permission_p = document.createElement("p");
        permission_p.className = "detail_p";
        permission_p.innerHTML = "<a href='#'>"+JSONdetail[ele_id]["permission"][c]+"</a>";
        model_permission.appendChild(permission_p);
    }
    var remark_p = document.createElement("p");
    remark_p.innerHTML = "<a href='#'>"+JSONdetail[ele_id]["remark"]+"</a>";
    remark_p.className = "detail_p";
    model_remark.appendChild(remark_p);
}

// 确认删除
function Confirm_delete() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>请选择需要删除的角色！</span>";
    }else {
        document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>确认要删除这"+String(myArray.length)+"个岗位吗？</span>"
    }
}

// 批量删除数据
function delete_data() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("close-model2").click();
        return
    }
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/system-setup/role_list.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("delete_list="+String(myArray)+"&page=1");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], 1);
                document.getElementById("check_all").checked = false;
                document.getElementById("close-model2").click();
            }
        }
    };
}
